import { Behaviour, Transform, SerilizeField, propertyEditor_Number } from "../gameengine";

export class MoveBehaviour extends Behaviour {

    duration: number = 2000;

    @SerilizeField(propertyEditor_Number)
    targetX: number = 0;

    @SerilizeField(propertyEditor_Number)
    targetY: number = 0;

    private duringTime = 0;

    private startX: number = 0;
    private startY: number = 0;

    onStart() {
        const transform = this.displayObject.getScript(Transform);
        this.startX = transform.x;
        this.startY = transform.y;
    }

    onUpdate(advancedTime: number) {

        function ease(x: number) {
            return -(x - 1) * (x - 1) + 1;
        }

        function ease2(x: number) {
            return x;
        }

        this.duringTime += advancedTime;

        let timeRate = this.duringTime / this.duration;

        if (timeRate > 1) {
            timeRate = 1;
        }
        timeRate = ease2(timeRate)
        const transform = this.displayObject.getScript(Transform);
        transform.x = (this.targetX - this.startX) * timeRate + this.startX;
        transform.y = (this.targetY - this.startY) * timeRate + this.startY;

        if (timeRate >= 1) {
            this.displayObject.removeScript(this);
        }
    }
}